﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using ESRI.ArcGIS.Geodatabase;
using System.Windows.Forms;
using System.Xml.Serialization;

namespace PlanningGIS.Product
{
    public abstract class CommonClass
    {
        /// <summary>
        /// 获取类的字段
        /// </summary>
        /// <param name="bindingAttr"></param>
        /// <returns></returns>
        public static List<string> getFields(Type pType,BindingFlags bindingAttr = BindingFlags.Default)
        {
            List<string> fields = new List<string>();

            
            FieldInfo[] fieldInfos = pType.GetFields();

            foreach (FieldInfo fieldInfoTemp in fieldInfos)
            {
                if (bindingAttr == BindingFlags.Public && fieldInfoTemp.IsPublic == true)
                {
                    fields.Add(fieldInfoTemp.Name);
                }
                else if (bindingAttr == BindingFlags.NonPublic && fieldInfoTemp.IsPrivate == true)
                {
                    fields.Add(fieldInfoTemp.Name);
                }
                else if (bindingAttr == BindingFlags.Default )
                {
                    fields.Add(fieldInfoTemp.Name);
                }
            }

            return fields;
        }

        /// <summary>
        /// 获取类的属性
        /// </summary>
        /// <param name="bindingAttr"></param>
        /// <returns></returns>
        public static List<string> getProperties(Type pType, BindingFlags bindingAttr = BindingFlags.Default)
        {
            List<string> fields = new List<string>();


            PropertyInfo[] propertyInfos = pType.GetProperties();

            foreach (PropertyInfo propertyInfoTemp in propertyInfos)
            {
                if (bindingAttr == BindingFlags.Public)
                {
                    fields.Add(propertyInfoTemp.Name);
                }
                else if (bindingAttr == BindingFlags.NonPublic)
                {
                    fields.Add(propertyInfoTemp.Name);
                }
                else if (bindingAttr == BindingFlags.Default)
                {
                    fields.Add(propertyInfoTemp.Name);
                }
            }

            return fields;
        }

        /// <summary>
        /// 判断字符串是否为字段名称
        /// </summary>
        /// <param name="pName"></param>
        /// <param name="bindingAttr"></param>
        /// <returns></returns>
        public static bool isAField(Type pType, string pName, BindingFlags bindingAttr = BindingFlags.Default)
        {
            foreach (string stringTemp in CommonClass.getFields(pType, bindingAttr))
            {
                if (pName == stringTemp) { return true; }
            }

            return false;
        }

        /// <summary>
        /// 判断字符串是否为属性名称
        /// </summary>
        /// <param name="pName"></param>
        /// <param name="bindingAttr"></param>
        /// <returns></returns>
        public static bool isAProperty(Type pType, string pName, BindingFlags bindingAttr = BindingFlags.Default)
        {
            foreach (string stringTemp in CommonClass.getProperties(pType, bindingAttr))
            {
                if (pName == stringTemp) { return true; }
            }

            return false;
        }


        /// <summary>
        /// 获取类的函数（方法等）
        /// </summary>
        /// <param name="bindingAttr"></param>
        /// <returns></returns>
        public static List<string> getMethods(Type pType, BindingFlags bindingAttr = BindingFlags.Default)
        {
            List<string> methods = new List<string>();

            MethodInfo[] methodInfos = pType.GetMethods(bindingAttr);

            foreach (MethodInfo methodInfo in methodInfos)
            {
                methods.Add(methodInfo.Name);
            }

            return methods;
        }

        /// <summary>
        /// 判断字符串是否为方法名称
        /// </summary>
        /// <param name="pName"></param>
        /// <param name="bindingAttr"></param>
        /// <returns></returns>
        public static bool isAMethod(Type pType, string pName, BindingFlags bindingAttr = BindingFlags.Default)
        {
            foreach (string stringTemp in CommonClass.getMethods(pType, bindingAttr))
            {
                if (pName == stringTemp) { return true; }
            }

            return false;
        }
        /// <summary>
        /// 获取一个全球惟一码
        /// 作者：徐红波
        /// 时间：2012-12-11
        /// </summary>
        /// <param name="likeArcGISGUIDFormat">是否采用ArcGIS GUID的格式,形如“71d5e390-4e76-43ef-b8ae-c0ece12d5939”</param>
        /// <returns></returns>
        public static string GetGUID(bool likeArcGISGUIDFormat = false)
        {
            string guid = "";

            guid = Guid.NewGuid().ToString();

            if (likeArcGISGUIDFormat == true)
            {
            }
            else
            {
                guid = guid.Replace("-", "");
            }

            return guid;
        }
        /// <summary>
        /// 创建要素类 根据要素类定义类创建
        /// 作者：徐红波
        /// 时间：2012-11-26
        /// </summary>
        /// <param name="pFeatWorkspace"></param>
        /// <param name="pFeatDataset"></param>
        /// <param name="pFeatureclassDef"></param>
        /// <returns></returns>
        public static IFeatureClass CreateFeatCls(IFeatureWorkspace pFeatWorkspace, IFeatureDataset pFeatDataset, PlanningGISFeatureclass pFeatureclassDef)
        {
            List<string>   FieldList= new List<string>();
            try
            {
                IFeatureClass featureClass = null;
                IFields fields = new FieldsClass();
                IFieldsEdit fieldsEdit = fields as IFieldsEdit;
                IField field = null;
                IFieldEdit2 fieldEdit = null;
                string shapeFieldName = pFeatureclassDef.shapeFieldName;
                string shapeFieldAlias = "";
                int i = 0;

                for (i = 0; i < pFeatureclassDef.fields.Count; i++)
                {


                    field = new FieldClass();
                    fieldEdit = field as IFieldEdit2;
                    fieldEdit.Name_2 = pFeatureclassDef.fields[i].name;
                    fieldEdit.AliasName_2 = pFeatureclassDef.fields[i].aliasName;
                    fieldEdit.Type_2 = pFeatureclassDef.fields[i].fieldType;
                    fieldEdit.Scale_2 = pFeatureclassDef.fields[i].scale;
                    if (pFeatureclassDef.fields[i].defaultValue != null) { fieldEdit.DefaultValue_2 = pFeatureclassDef.fields[i].defaultValue; }
                    if (pFeatureclassDef.fields[i].length != null) { fieldEdit.Length_2 = pFeatureclassDef.fields[i].length; }
                    fieldEdit.Precision_2 = pFeatureclassDef.fields[i].precision;

                    if (pFeatureclassDef.fields[i].fieldType == esriFieldType.esriFieldTypeGeometry)
                    {
                        shapeFieldName = pFeatureclassDef.fields[i].name;
                        shapeFieldAlias = pFeatureclassDef.fields[i].aliasName;
                        fieldEdit.GeometryDef_2 = pFeatureclassDef.fields[i].geometryDef;
                    }
                    if (!FieldList.Contains(field.Name))
                    {
                        fieldsEdit.AddField(field);
                    }
                    FieldList.Add(field.Name);
                }

                if (pFeatDataset != null)
                {
                    featureClass =
                        pFeatDataset.CreateFeatureClass(pFeatureclassDef.name.Replace('.', '_').Replace(' ', '_').Replace('-', '_'), fields, null, null, pFeatureclassDef.featureType, shapeFieldName, "");
                }
                else
                {
                    featureClass =
                        pFeatWorkspace.CreateFeatureClass(pFeatureclassDef.name.Replace('.', '_').Replace(' ', '_').Replace('-', '_'), fields, null, null, pFeatureclassDef.featureType, shapeFieldName, "");
                }

                ESRI.ArcGIS.Geodatabase.IClassSchemaEdit3 classSchemEdit = featureClass as IClassSchemaEdit3;
                classSchemEdit.AlterAliasName(pFeatureclassDef.aliasName);

                return featureClass;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + "\n"
                    + ex.Source + "\n"
                    + ex.StackTrace);
                return null;
            }
        }
        /// <summary>
        /// 获取类型中指定成员的属性的XMLAttribute名称
        /// 作者：徐红波
        /// 时间：2012-11-26
        /// </summary>
        /// <param name="pType"></param>
        /// <param name="pAttributeName"></param>
        /// <returns></returns>
        public static string getXMLAttributeInfo(Type pType, string pAttributeName)
        {
            string attributeInfo = "";
            MemberInfo[] menberInfos = pType.GetMember(pAttributeName);

            bool isDef = System.Attribute.IsDefined(menberInfos[0], typeof(XmlAttributeAttribute));

            if (isDef)
            {
                XmlAttributeAttribute xmlAttr =
                                 (XmlAttributeAttribute)System.Attribute.GetCustomAttribute(
                                                    menberInfos[0], typeof(XmlAttributeAttribute));
                if (xmlAttr != null)
                {
                    attributeInfo = xmlAttr.AttributeName;
                }
            }


            return attributeInfo;
        }
    }
}
